package xyz.weizhi.tanhua.appserver.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import xyz.weizhi.tanhua.appserver.interceptor.UserHolder;
import xyz.weizhi.tanhua.autoconfig.Tempalte.OssTemplate;
import xyz.weizhi.tanhua.dubbo.api.*;
import xyz.weizhi.tanhua.model.domain.Question;
import xyz.weizhi.tanhua.model.domain.Settings;
import xyz.weizhi.tanhua.model.domain.UserInfo;
import xyz.weizhi.tanhua.model.vo.PageResult;
import xyz.weizhi.tanhua.model.vo.SettingsVo;

import java.io.IOException;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/10/24 0024 16:28
 */
@Service
public class SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private OssTemplate ossTemplate;
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        // 获取用户id
        Long userId = UserHolder.getId();
        vo.setId(userId);
        // 获取用户手机号码
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        // 获取设置信息
        Settings userSetting = settingsApi.findByUserId(userId);
        // 获取用户陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 处理数据
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        if(userSetting != null) {
            vo.setGonggaoNotification(userSetting.getGonggaoNotification());
            vo.setPinglunNotification(userSetting.getPinglunNotification());
            vo.setLikeNotification(userSetting.getLikeNotification());
        }
        return vo;
    }

    public void saveQuestion(String content) {
        // 获取用户id
        Long userId = UserHolder.getId();
        // 查询用户陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 如果不存在保存问题，存在更新问题
        if (question==null){
            // 保存问题
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //更新问题
            question.setTxt(content);
            questionApi.updateById(question);
        }
    }

    public void updateSetting( Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        // 获取用户id
        Long userId = UserHolder.getId();
        // 查询用户通知设置
        Settings userSetting = settingsApi.findByUserId(userId);
        if (userSetting==null){
            userSetting= new Settings();
            userSetting.setUserId(userId);
            userSetting.setGonggaoNotification(gonggaoNotification);
            userSetting.setLikeNotification(likeNotification);
            userSetting.setPinglunNotification(pinglunNotification);
            settingsApi.save(userSetting);
        }else {
            userSetting.setGonggaoNotification(gonggaoNotification);
            userSetting.setLikeNotification(likeNotification);
            userSetting.setPinglunNotification(pinglunNotification);
            settingsApi.updateById(userSetting);
        }
    }

    public PageResult blacklist(int page, int pagesize) {
        Long userId = UserHolder.getId();
        IPage pages = blackListApi.findByUserId(userId, page, pagesize);
        PageResult pageResult = new PageResult(page,pagesize,pages.getTotal(),pages.getRecords());

        return pageResult;
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getId();
        blackListApi.delete(userId,blackUserId);
    }

    public void updatePhone(String newPhone) {
        // 获取用户id
        Long userId = UserHolder.getId();
        // 修改user表中的手机号
        userApi.updatePhone(userId,newPhone);
    }

    public void updateHeader(MultipartFile file) throws IOException {
        // 获取用户id
        Long userId = UserHolder.getId();
        // 上传头像到阿里云
        String url = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
        // 查询用户信息
        UserInfo userInfo = userInfoApi.findById(userId);
        userInfo.setAvatar(url);
        userInfoApi.update(userInfo);
    }
}
